package com.ophiux.schedule;

import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

/**
 * @desc: 预定日常生产者
 * @author: hhl
 * @date：2023/8/17 9:20
 *
 * broker.conf配置里面可以配置时间
 * messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
 */
public class ScheduleProducer {

    public static void main(String[] args) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {
        DefaultMQProducer producer = new DefaultMQProducer("ScheduleProducer");
        producer.setNamesrvAddr("127.0.0.1:9876");
        producer.start();

        for (int i = 0; i < 1; i++) {
            System.out.println(i);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("key", i);
            Message message = new Message("Schedule", "Tags", jsonObject.toJSONString().getBytes(StandardCharsets.UTF_8));
            //1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
//            message.setDelayTimeLevel(2);
            //10秒
            message.setDelayTimeMs(10 *1000L);

            producer.send(message);
            System.out.println("消息发送成功："+ LocalDateTime.now());
        }

        producer.shutdown();
    }
}
